package com.ysd.lis.service.lab.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.LabTextResult;
import com.ysd.lis.mapper.lab.LabTextResultMapper;
import com.ysd.lis.service.lab.LabTextResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 文字结果 服务实现类
 * </p>
 *
 * @author wanghao
 * @since 2024-01-24
 */
@Transactional
@Service
public class LabTextResultServiceImpl extends ServiceImpl<LabTextResultMapper, LabTextResult> implements LabTextResultService {

    @Autowired
    private LabTextResultMapper labTextResultMapper;

    @Override
    public Result getTextResultByitemDictId(String itemNo, String rptGroup) {
        LambdaQueryWrapper<LabTextResult> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LabTextResult::getDelFlag, 0)
                .eq(LabTextResult::getItemNo, itemNo)
                .eq(LabTextResult::getRptGroup, rptGroup)
                .orderByAsc(LabTextResult::getSeq);
        List<LabTextResult> labTextResults = labTextResultMapper.selectList(queryWrapper);
        return Result.succ(1, "查询成功", labTextResults);
    }
    @Override
    public Result getTextResultByQuickICode(String itemNo, String rptGroup, String quickInputCode) {
        LambdaQueryWrapper<LabTextResult> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LabTextResult::getDelFlag, 0)
                .eq(LabTextResult::getItemNo, itemNo)
                .eq(LabTextResult::getRptGroup, rptGroup)
                .eq(LabTextResult::getQuickInputCode, quickInputCode)
                .orderByAsc(LabTextResult::getSeq);
        LabTextResult labTextResult = labTextResultMapper.selectOne(queryWrapper);
        return Result.succ(1, "查询成功", labTextResult);
    }

    @Override
    public Result saveOrUpdateTextResult(List<LabTextResult> labTextResults) {
        List<LabTextResult> collect = labTextResults.stream().filter(item -> StringUtils.isNotBlank(item.getResult())).collect(Collectors.toList());

        boolean b = this.saveOrUpdateBatch(collect);
        if (b) {
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }
    }

    @Override
    public Result batchDeleteTextResult(List<LabTextResult> labTextResults) {

        List<String> ids = labTextResults.stream().map(LabTextResult::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }
}
